package com.qiantu.phone.ui.uiutil;

import android.graphics.drawable.Drawable;

import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.LinkageBean;
import com.qiantu.api.entity.LinkageNewBean;
import com.qiantu.api.entity.LinkageStartStop;
import com.qiantu.api.entity.SceneDetailBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppApplication;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @ClassName SceneUtil
 * @Description TODO
 * @Author ShiJunqiang
 * @Date 2021/12/6 17:55
 * @Version 1.0
 */
public class LLSceneLinkageUtil {
    public static void addDelayTask(SceneDetailBean editBean, Integer longTime, int position) {
        if (editBean.getSceneTasks() == null) {
            editBean.setSceneTasks(new ArrayList<>());
        }
        SceneDetailBean.SceneDelayedTask delayedTask = new SceneDetailBean.SceneDelayedTask();
        delayedTask.setLongTime(longTime);
        SceneDetailBean.SceneTaskBean sceneTaskBean = new SceneDetailBean.SceneTaskBean();
        sceneTaskBean.setSceneTaskType(2);
        sceneTaskBean.setSceneDelayedTask(delayedTask);
        sceneTaskBean.setTaskBriefing(makeTaskBriefing(sceneTaskBean));
        sceneTaskBean.setName(AppApplication.getInstance().getResources().getString(R.string.delay_interval));
        sceneTaskBean.setImage("icon_intelligent_action_delay");
        if (position < 0) {
            editBean.getSceneTasks().add(sceneTaskBean);
        } else {
            editBean.getSceneTasks().add(position, sceneTaskBean);
        }
//        sceneTaskBean.setSort(editBean.getSceneTasks().indexOf(sceneTaskBean) + 1);
    }

    public static void addDelayTask(LinkageBean editBean, int longTime, int position) {
        if (editBean.getLinkageTasks() == null) {
            editBean.setLinkageTasks(new ArrayList<>());
        }
        LinkageBean.LinkageTask linkageTask = new LinkageBean.LinkageTask();
        linkageTask.setTaskType(2);
        linkageTask.setLongTime(longTime);
        linkageTask.setBriefing("delay");
        linkageTask.setImageUrl("icon_intelligent_action_delay");
        if (position < 0) {
            editBean.getLinkageTasks().add(linkageTask);
        } else {
            editBean.getLinkageTasks().add(position, linkageTask);
        }
//        linkageTask.setSort(editBean.getLinkageTasks().indexOf(linkageTask) + 1);
    }

    public static SceneDetailBean.SceneTaskBean addDeviceInstruct(SceneDetailBean editBean, DeviceBean deviceBean, Map<String, String> status, int position) {
        if (editBean.getSceneTasks() == null) {
            editBean.setSceneTasks(new ArrayList<>());
        }
        SceneDetailBean.SceneTaskBean sceneTaskBean = new SceneDetailBean.SceneTaskBean();
        //先设置个本地的SerialNo，为了编辑找到对应的数据
        sceneTaskBean.setSceneTaskSerialNo(UUID.randomUUID().toString());
        sceneTaskBean.setName(deviceBean.getName());
        sceneTaskBean.setImage(deviceBean.getImageUrl());
        if (position < 0) {
            editBean.getSceneTasks().add(sceneTaskBean);
        } else {
            editBean.getSceneTasks().add(position, sceneTaskBean);
        }
        if(deviceBean.getDeviceGroup() == 1){
            sceneTaskBean.setSceneTaskType(5);
            SceneDetailBean.SceneDeviceGroupInstruct instructBean = new SceneDetailBean.SceneDeviceGroupInstruct();
            instructBean.setDeviceGroupSerialNo(deviceBean.getDeviceSerialNo());
            instructBean.setStateData(new SceneDetailBean.DeviceItemStateDataBean(null, status));
            instructBean.setTriggerTime(0);
            sceneTaskBean.setSceneDeviceGroupInstruct(instructBean);
        }else{
            sceneTaskBean.setSceneTaskType(1);
            SceneDetailBean.SceneDeviceInstructBean instructBean = new SceneDetailBean.SceneDeviceInstructBean();
            instructBean.setDeviceItemSerialNo(deviceBean.getDeviceSerialNo());
            instructBean.setDeviceItemStateData(new SceneDetailBean.DeviceItemStateDataBean(null, status));
            instructBean.setTriggerTime(0);
            instructBean.setDeviceType(deviceBean.getDeviceType());
            sceneTaskBean.setSceneDeviceInstruct(instructBean);
        }
        sceneTaskBean.setTaskBriefing(makeTaskBriefing(sceneTaskBean));

        return sceneTaskBean;
//        sceneTaskBean.setSort(editBean.getSceneTasks().indexOf(sceneTaskBean) + 1);
    }

    public static LinkageBean.LinkageTask addDeviceInstruct(LinkageBean editBean, DeviceBean deviceBean, Map<String, String> status, int position) {
        if (editBean.getLinkageTasks() == null) {
            editBean.setLinkageTasks(new ArrayList<>());
        }
        LinkageBean.LinkageTask linkageTask = new LinkageBean.LinkageTask();
        //先设置个本地的SerialNo，为了编辑找到对应的数据
        linkageTask.setTaskSerialNo(UUID.randomUUID().toString());
        linkageTask.setTriggerTime(0);
        linkageTask.setBriefing(deviceBean.getName());
        linkageTask.setImageUrl(deviceBean.getImageUrl());
        linkageTask.setDeviceType(deviceBean.getDeviceType());
        if (position < 0) {
            editBean.getLinkageTasks().add(linkageTask);
        } else {
            editBean.getLinkageTasks().add(position, linkageTask);
        }
        if(deviceBean.getDeviceGroup() == 1){
            linkageTask.setTaskType(9);
            linkageTask.setDeviceGroupSerialNo(deviceBean.getDeviceSerialNo());
            linkageTask.setStateData(new LinkageBean.LinkageTask.DeviceStateDataDTO(status));

        }else{
            linkageTask.setTaskType(1);
            linkageTask.setDeviceSerialNo(deviceBean.getDeviceSerialNo());
            linkageTask.setDeviceStateData(new LinkageBean.LinkageTask.DeviceStateDataDTO(status));
        }
        return linkageTask;
//        linkageTask.setSort(editBean.getLinkageTasks().indexOf(linkageTask) + 1);
    }

    public static LinkageBean.LinkageTask addDeviceInstructNew(LinkageNewBean editBean, DeviceBean deviceBean, Map<String, String> status, int position) {
        if (editBean.getLinkageTasks() == null) {
            editBean.setLinkageTasks(new ArrayList<>());
        }
        LinkageBean.LinkageTask linkageTask = new LinkageBean.LinkageTask();
        //先设置个本地的SerialNo，为了编辑找到对应的数据
        linkageTask.setTaskSerialNo(UUID.randomUUID().toString());
        linkageTask.setTriggerTime(0);
        linkageTask.setBriefing(deviceBean.getName());
        linkageTask.setImageUrl(deviceBean.getImageUrl());
        linkageTask.setDeviceType(deviceBean.getDeviceType());
        if (position < 0) {
            editBean.getLinkageTasks().add(linkageTask);
        } else {
            editBean.getLinkageTasks().add(position, linkageTask);
        }
        if(deviceBean.getDeviceGroup() == 1){
            linkageTask.setTaskType(9);
            linkageTask.setDeviceGroupSerialNo(deviceBean.getDeviceSerialNo());
            linkageTask.setStateData(new LinkageBean.LinkageTask.DeviceStateDataDTO(status));

        }else{
            linkageTask.setTaskType(1);
            linkageTask.setDeviceSerialNo(deviceBean.getDeviceSerialNo());
            linkageTask.setDeviceStateData(new LinkageBean.LinkageTask.DeviceStateDataDTO(status));
        }
        return linkageTask;
//        linkageTask.setSort(editBean.getLinkageTasks().indexOf(linkageTask) + 1);
    }

    public static String getTimingValue(LinkageBean.LinkageCondition linkageCondition) {
        if (linkageCondition != null) {
            if (linkageCondition.getTimingType() == 1) {
                StringBuilder stringBuilder = new StringBuilder();
                if (linkageCondition.isMonday())
                    stringBuilder.append(AppApplication.getInstance().getString(R.string.monday)).append("、");
                if (linkageCondition.isTuesday())
                    stringBuilder.append(AppApplication.getInstance().getString(R.string.tuesday)).append("、");
                if (linkageCondition.isWednesday())
                    stringBuilder.append(AppApplication.getInstance().getString(R.string.wednesday)).append("、");
                if (linkageCondition.isThursday())
                    stringBuilder.append(AppApplication.getInstance().getString(R.string.thursday)).append("、");
                if (linkageCondition.isFriday())
                    stringBuilder.append(AppApplication.getInstance().getString(R.string.friday)).append("、");
                if (linkageCondition.isSaturday())
                    stringBuilder.append(AppApplication.getInstance().getString(R.string.saturday)).append("、");
                if (linkageCondition.isSunday())
                    stringBuilder.append(AppApplication.getInstance().getString(R.string.sunday)).append("、");
                return AppApplication.getInstance().getString(R.string.every_week_holder, stringBuilder.substring(0, stringBuilder.length() - 1));
            } else {
                return AppApplication.getInstance().getString(R.string.no_repetition);
            }
        }
        return "";
    }

    public static String getTimingValueNew(LinkageNewBean.LinkageTimeCondition linkageCondition) {
        if (linkageCondition != null) {
            if(linkageCondition.getRepeatTypeId() == 1){
                return AppApplication.getInstance().getString(R.string.no_repetition);
            }else if(linkageCondition.getRepeatTypeId() == 2){
                return AppApplication.getInstance().getString(R.string.linkage_time_everyday);
            }else if(linkageCondition.getRepeatTypeId() == 3){
                return AppApplication.getInstance().getString(R.string.linkage_time_weekday);
            }else if(linkageCondition.getRepeatTypeId() == 4){
                return AppApplication.getInstance().getString(R.string.linkage_time_weekend);
            }else if(linkageCondition.getRepeatTypeId() == 5){
                List<Integer> times = linkageCondition.getRepeatWeekDays();
                StringBuilder stringBuilder = new StringBuilder();
                for(int i = 0;i < times.size();i++){
                    Integer time = times.get(i);
                    if (time == 1)
                        stringBuilder.append(AppApplication.getInstance().getString(R.string.monday)).append("、");
                    if (time == 2)
                        stringBuilder.append(AppApplication.getInstance().getString(R.string.tuesday)).append("、");
                    if (time == 3)
                        stringBuilder.append(AppApplication.getInstance().getString(R.string.wednesday)).append("、");
                    if (time == 4)
                        stringBuilder.append(AppApplication.getInstance().getString(R.string.thursday)).append("、");
                    if (time == 5)
                        stringBuilder.append(AppApplication.getInstance().getString(R.string.friday)).append("、");
                    if (time == 6)
                        stringBuilder.append(AppApplication.getInstance().getString(R.string.saturday)).append("、");
                    if (time == 0)
                        stringBuilder.append(AppApplication.getInstance().getString(R.string.sunday)).append("、");
                }
                return AppApplication.getInstance().getString(R.string.every_week_holder, stringBuilder.substring(0, stringBuilder.length() - 1));
            }else if(linkageCondition.getRepeatTypeId() == 6){
                return linkageCondition.getRepeatFromTime() + " - " + linkageCondition.getRepeatToTime();
            }
        }
        return "";
    }

    public static String getTimingValueNew(LinkageNewBean.LinkageSunCondition linkageCondition) {
        if (linkageCondition != null) {
            if(linkageCondition.getRepeatTypeId() == 1){
                return AppApplication.getInstance().getString(R.string.no_repetition);
            }else if(linkageCondition.getRepeatTypeId() == 2){
                return AppApplication.getInstance().getString(R.string.linkage_time_everyday);
            }else if(linkageCondition.getRepeatTypeId() == 3){
                return AppApplication.getInstance().getString(R.string.linkage_time_weekday);
            }else if(linkageCondition.getRepeatTypeId() == 4){
                return AppApplication.getInstance().getString(R.string.linkage_time_weekend);
            }else if(linkageCondition.getRepeatTypeId() == 5){
                List<Integer> times = linkageCondition.getRepeatWeekDays();
                StringBuilder stringBuilder = new StringBuilder();
                for(int i = 0;i < times.size();i++){
                    Integer time = times.get(i);
                    if (time == 1)
                        stringBuilder.append(AppApplication.getInstance().getString(R.string.monday)).append("、");
                    if (time == 2)
                        stringBuilder.append(AppApplication.getInstance().getString(R.string.tuesday)).append("、");
                    if (time == 3)
                        stringBuilder.append(AppApplication.getInstance().getString(R.string.wednesday)).append("、");
                    if (time == 4)
                        stringBuilder.append(AppApplication.getInstance().getString(R.string.thursday)).append("、");
                    if (time == 5)
                        stringBuilder.append(AppApplication.getInstance().getString(R.string.friday)).append("、");
                    if (time == 6)
                        stringBuilder.append(AppApplication.getInstance().getString(R.string.saturday)).append("、");
                    if (time == 0)
                        stringBuilder.append(AppApplication.getInstance().getString(R.string.sunday)).append("、");
                }
                return AppApplication.getInstance().getString(R.string.every_week_holder, stringBuilder.substring(0, stringBuilder.length() - 1));
            }else if(linkageCondition.getRepeatTypeId() == 6){
                return linkageCondition.getRepeatFromTime() + " - " + linkageCondition.getRepeatToTime();
            }
        }
        return "";
    }

    public static Drawable getTaskIcon(int sceneTaskType) {
        switch (sceneTaskType) {
            case 1:
                return AppApplication.getInstance().getThemeDrawable(R.attr.icon_intelligent_device);
            case 2:
                return AppApplication.getInstance().getThemeDrawable(R.attr.icon_intelligent_action_delay);
            case 3:
                return AppApplication.getInstance().getThemeDrawable(R.attr.icon_intelligent_action_push);
            case 4:
                return AppApplication.getInstance().getThemeDrawable(R.attr.icon_intelligent_linkage);
            case 5:
                return AppApplication.getInstance().getThemeDrawable(R.attr.icon_intelligent_scenes);
        }
        return AppApplication.getInstance().getThemeDrawable(R.attr.icon_intelligent_device);
    }

    public static List<String> makeTaskBriefing(SceneDetailBean.SceneTaskBean sceneTaskBean) {
        List<String> taskBriefing = sceneTaskBean.getTaskBriefing();
        if (taskBriefing == null) {
            taskBriefing = new ArrayList<>();
        }
        switch (sceneTaskBean.getSceneTaskType()) {
            case 1: {//设备动作的话，不在这边，根据指令生成
                return taskBriefing;
            }
            case 2: {
                int longTime = sceneTaskBean.getSceneDelayedTask().getLongTime();
                taskBriefing.clear();
                taskBriefing.add(AppApplication.getInstance().getString(R.string.delay_execute_holder, mill2str(longTime)));
                break;
            }
            case 3: {//发送消息
//                return taskBriefing;
                taskBriefing.clear();
                taskBriefing.add(sceneTaskBean.getScenePushMessage().getMessage());
                break;
            }
            case 4: {//联动启停
//                return taskBriefing;
                taskBriefing.clear();
                List<LinkageStartStop> linkageStartStops = sceneTaskBean.getLinkageStartStops();
                int disableCount = 0;
                int enableCount = 0;
                for (LinkageStartStop linkageStartStop : linkageStartStops) {
                    if (linkageStartStop.isDisable()) {
                        disableCount++;
                    } else {
                        enableCount++;
                    }
                }
                taskBriefing.add(AppApplication.getInstance().getString(R.string.linkage_start_stop_count_holder, enableCount, disableCount));
                break;
            }
        }
        return taskBriefing;
    }

    public static String makeTaskBriefing(LinkageBean.LinkageTask linkageTask) {
        switch (linkageTask.getTaskType()) {
            case 1: {//设备动作的话，不在这边，根据指令生成
                return "";
            }
            case 2: {//延时
                int longTime = linkageTask.getLongTime();
                return AppApplication.getInstance().getString(R.string.delay_execute_holder, mill2str(longTime));
            }
            case 3: {//发送消息
                return linkageTask.getMessage();
            }
            case 4: {//联动启停
                List<LinkageStartStop> linkageStartStops = linkageTask.getLinkageStartStop();
                int disableCount = 0;
                int enableCount = 0;
                for (LinkageStartStop linkageStartStop : linkageStartStops) {
                    if (linkageStartStop.isDisable()) {
                        disableCount++;
                    } else {
                        enableCount++;
                    }
                }
                return AppApplication.getInstance().getString(R.string.linkage_start_stop_count_holder, enableCount, disableCount);
            }
            case 5: {//一键场景
                int count = linkageTask.getSceneList() == null ? 0 : linkageTask.getSceneList().size();
                return AppApplication.getInstance().getString(R.string.scene_execute_count_holder, count);
            }
        }
        return "";
    }

    public static String mill2str(int millisecond) {
        String[] TIME_NUM = {"00", "01", "02", "03", "04", "05", "06", "07", "08", "09"};
//        int hour= millisecond/1000/3600;
        int min = millisecond / 1000 % 3600 / 60;
        int s = millisecond / 1000 % 60;
        String result = "";
//        if (hour>=0 && hour<10){
//            result+=TIME_NUM[hour];
//        }else {
//            result+=hour;
//        }
//        result+=":";
        if (min >= 0 && min < 10) {
            result += TIME_NUM[min];
        } else {
            result += min;
        }
        result += ":";
        if (s >= 0 && s < 10) {
            result += TIME_NUM[s];
        } else {
            result += s;
        }
        return result;
    }

    public static String getLinkageUnit(int conditionType){
        if(conditionType == 2){
            return "℃";
        }else if(conditionType == 3){
            return "%";
        }else if(conditionType == 12){
            return "dB";
        }else if(conditionType == 13){
            return "lux";
        }else if(conditionType == 14){
            return "μg/m³";
        }
        return "";
    }

    public static int getLinkageImage(int conditionType){
        if(conditionType == 1){
            return R.attr.icon_intelligent_condition_timing;
        }else if(conditionType == 2){
            return R.attr.icon_intelligent_environment_temperature;
        }else if(conditionType == 3){
            return R.attr.icon_intelligent_environment_humidity;
        }else if(conditionType == 5){
            return R.attr.icon_intelligent_environment_pm25;
        }else if(conditionType == 7){
            return R.attr.icon_intelligent_environment_location;
        }else if(conditionType == 9){
            return R.attr.icon_intelligent_environment_pir;
        }else if(conditionType == 10){
            return R.attr.icon_intelligent_environment_pir;
        }else if(conditionType == 12){
            return R.attr.icon_intelligent_environment_noise;
        }else if(conditionType == 13){
            return R.attr.icon_intelligent_environment_illumination;
        }else if(conditionType == 14){
            return R.attr.icon_intelligent_environment_pm2_5;
        }else if(conditionType == 16){
            return R.attr.icon_intelligent_environment_gatemagnetism_linkage;
        }else if(conditionType == 17){
            return R.attr.icon_intelligent_environment_gas_linkage;
        }else if(conditionType == 18){
            return R.attr.icon_intelligent_environment_smog_linkage;
        }else if(conditionType == 19){
            return R.attr.icon_intelligent_environment_trigger_linkage;
        }else if(conditionType == 20){
            return R.attr.icon_intelligent_environment_alarm_linkage;
        }else if(conditionType == 21){
            return R.attr.icon_intelligent_environment_infrared_linkage;
        }else if(conditionType == 22){
            return R.attr.icon_intelligent_environment_correlation_linkage;
        }else if(conditionType == 23){
            return R.attr.icon_intelligent_environment_shake_linkage;
        }else if(conditionType == 24){
            return R.attr.icon_intelligent_environment_curtain_linkage;
        }
        return R.attr.icon_intelligent_condition_timing;
    }

    public static String getLinkageImageStr(int conditionType){
        if(conditionType == 1){
            return "";
        }else if(conditionType == 2){
            return "icon_intelligent_environment_temperature";
        }else if(conditionType == 3){
            return "";
        }else if(conditionType == 5){
            return "";
        }else if(conditionType == 7){
            return "";
        }else if(conditionType == 9){
            return "";
        }else if(conditionType == 10){
            return "";
        }else if(conditionType == 12){
            return "icon_intelligent_environment_noise";
        }else if(conditionType == 13){
            return "icon_intelligent_environment_illumination";
        }else if(conditionType == 14){
            return "icon_intelligent_environment_pm2_5";
        }else if(conditionType == 16){
            return "";
        }else if(conditionType == 17){
            return "";
        }else if(conditionType == 18){
            return "";
        }else if(conditionType == 19){
            return "";
        }else if(conditionType == 20){
            return "";
        }else if(conditionType == 21){
            return "";
        }else if(conditionType == 22){
            return "";
        }else if(conditionType == 23){
            return "";
        }else if(conditionType == 24){
            return "";
        }
        return "";
    }

    public static int getLinkageEnvrionmentName(int conditionType){
        if(conditionType == 1){
            return R.string.time;
        }else if(conditionType == 2){
            return R.string.temperature;
        }else if(conditionType == 3){
            return R.string.humidity;
        }else if(conditionType == 5){
            return R.string.outdoor_pm25;
        }else if(conditionType == 7){
            return R.string.location;
        }else if(conditionType == 9){
            return R.string.human_body_induction;
        }else if(conditionType == 10){
            return R.string.human_presence_induction;
        }else if(conditionType == 12){
            return R.string.noise;
        }else if(conditionType == 13){
            return R.string.illuminance;
        }else if(conditionType == 14){
            return R.string.pm25;
        }else if(conditionType == 16){
            return R.string.door_contact;
        }else if(conditionType == 17){
            return R.string.toxic_gas;
        }else if(conditionType == 18){
            return R.string.smoke_alarm;
        }else if(conditionType == 19){
            return R.string.trigger_switch;
        }else if(conditionType == 20){
            return R.string.alarm_switch;
        }else if(conditionType == 21){
            return R.string.humanbody_senoro;
        }else if(conditionType == 22){
            return R.string.infrared_contrast;
        }else if(conditionType == 23){
            return R.string.vibration_sensing;
        }else if(conditionType == 24){
            return R.string.infrared_curtain;
        }
        return R.attr.icon_intelligent_condition_timing;
    }

    public static String getLinkageState(int conditionType){
        if(conditionType == 2){
            return "temperature";
        }else if(conditionType == 3){
            return "humidity";
        }else if(conditionType == 12){
            return "noise";
        }else if(conditionType == 13){
            return "illuminance";
        }else if(conditionType == 14){
            return "pm2_5";
        }
        return "trigger";
    }

    public static int getStateActionName(int conditionType,int trigger){
        if(conditionType == 10 || conditionType == 21 || conditionType == 22 || conditionType == 23 || conditionType == 24){
            if(trigger == 1){
                return R.string.somebody;
            }else if(trigger == 0){
                return R.string.nobody;
            }
        }else if(conditionType == 16){
            if(trigger == 1){
                return R.string.opendoor;
            }else if(trigger == 0){
                return R.string.closedoor;
            }
        }else if(conditionType == 17 || conditionType == 18 || conditionType == 19 || conditionType == 20){
            if(trigger == 1){
                return R.string.linkage_trigger;
            }else if(trigger == 0){
                return R.string.linkage_normal;
            }
        }
        return 0;
    }

    public static boolean compareVersion(String currentVersion, String upgradeVersion) {
        String[] currentVersionList = currentVersion.split("\\.");
        String[] upgradeVersionList = upgradeVersion.split("\\.");
        int size = Math.max(currentVersionList.length, upgradeVersionList.length);
        String[] currentVersionNew = new String[size];
        String[] upgradeVersionNew = new String[size];
        for (int i = 0; i < size; i++) {
            if (i < currentVersionList.length) {
                currentVersionNew[i] = currentVersionList[i];
            } else {
                currentVersionNew[i] = "0";
            }
            if (i < upgradeVersionList.length) {
                upgradeVersionNew[i] = upgradeVersionList[i];
            } else {
                upgradeVersionNew[i] = "0";
            }
        }
        if (size < 3)
            return false;
        for (int i = 0; i < size; i++) {
            int currentVersionNum = Integer.parseInt(currentVersionNew[i]);
            int upgradeVersionNum = Integer.parseInt(upgradeVersionNew[i]);
            if (upgradeVersionNum < currentVersionNum)
                return false;
            if (upgradeVersionNum > currentVersionNum)
                return true;
        }
        //相等
        return false;
    }
}
